//
//  CXWebDriver.m
//  Codex
//
//  Created by ChenJie on 01/02/2018.
//  Copyright © 2018 Mist. All rights reserved.
//

#import "CXWebDriver.h"

@interface CXWebDriver()

@property (nonatomic, strong) AFHTTPSessionManager *manager;
@property (nonatomic, strong, readwrite) NSDictionary<NSString *, NSString *> *requestHeaders;

@end

@implementation CXWebDriver

@synthesize requestHeaders = _requestHeaders;

#pragma mark - Public Methods

/**
 *
 */
- (NSURLSessionDataTask *)post:(NSString *)url
                    parameters:(NSDictionary *)parameters
                       success:(WebDriverSuccessCallback)success
                       failure:(WebDriverFailureCallback)failure {
    
    return [self post:url parameters:parameters progress:nil success:success failure:failure];
}

/**
 *
 */
- (NSURLSessionDataTask *)post:(NSString *)url
                    parameters:(NSDictionary *)parameters
                      progress:(WebDriverProgressCallback)progress
                       success:(WebDriverSuccessCallback)success
                       failure:(WebDriverFailureCallback)failure {
    
    return [self.manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        !progress ?: progress(uploadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        !success ?: success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        !failure ?: failure(error);
    }];
}

#pragma mark - Lifecycle Methods

+ (instancetype)sharedDriver {
    static CXWebDriver *driver;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        driver = [[CXWebDriver alloc] init];
    });
    return driver;
}

#pragma mark - Setter Methods

- (void)setRequestHeaders:(NSDictionary<NSString *,NSString *> *)requestHeaders {
    _requestHeaders = requestHeaders;
    
    [requestHeaders enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        [self.manager.requestSerializer setValue:obj forHTTPHeaderField:key];
    }];
}

#pragma mark - Getter Methods

- (AFHTTPSessionManager *)manager {
    if (!_manager) {
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes
        = [NSSet setWithArray:@[
                                @"application/json",
                                @"application/xml",
                                @"application/soap+xml",
                                @"text/json",
                                @"text/xml",
                                @"text/javascript",
                                @"text/html",
                                @"text/plain",
                                ]];
        _manager = manager;
    }
    return _manager;
}

- (NSDictionary<NSString *, NSString *> *)requestHeaders {
    if (!_requestHeaders) {
        _requestHeaders = self.manager.requestSerializer.HTTPRequestHeaders;
    }
    return _requestHeaders;
}

@end
